<?php

if (!$character['reputation'])
	{echo "<td>Reputations could not be found</td>";}

else

{
	echo "<td>Updating reputations </td>";
	foreach ($character['reputation'] as $rep)

	{
		if ($rep['value']>0 && $rep['standing']>0)//reputation is neutral or better
			{
			//fill variables
			$factionId = $rep['id'];
			$factionName = $rep['name'];
			$standing = $rep['standing'];
			$repValue = $rep['value'];
			$repMax = $rep['max'];

			//find existing faction standing
			$sqlOldRep = "SELECT standing, value FROM char_history_rep cr INNER JOIN char_history c ON
	cr.char_history_id = c.char_history_id where cr.faction_id = ? AND c.char_id = ?
	ORDER BY cr.char_history_id DESC
	LIMIT 0,1";

			$pdo->bound_query($sqlOldRep,array($factionId,$char_id),'All');
			
			$sqlOldRepRow = $pdo->result;

			if ($sqlOldRepRow[0]==$standing && $sqlOldRepRow[1]==$repValue)
				{
				//echo "Rep for $factionName not updated.<br/></td>";
				}
				//if the standing is the same, do not insert

			//if the standing is different or the faction does not exist, insert it
			else
			{
				$sqlInsertReps = "INSERT INTO char_history_rep (char_history_id, faction_id, name, standing, value, max, datestamp, 		userstamp) VALUES (?,?,?,?,?,?,
		(select now()), 1)";

				$pdo->bound_query($sqlInsertReps,array($charHistoryId, $factionId, $factionName, $standing, $repValue, $repMax),'None','Insert Reputations');
				
				$insertreps = $pdo->rownum;
				if (empty($insertreps))
					{echo "<td>Character reputations record could not be inserted<br/></td>";}
				else
					{echo "<td>Reputation for $factionName updated<br/></td>";}

			}

		}
	}


}





?>